#include "View.h"
#include "../Model/Model.h"
#include "../Model/Library.h"
#include "../Model/LibraryRecord.h"
#include "../Model/ModelMaker.h"
#include "LibraryView.h"
#include <ncurses.h>
#include <menu.h>
#include <locale.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include "../IO/AudioModule.h"
#include "AMView.h"
#include <stdexcept>
#include "Settings.h"


View::View(Library & lib) : lv(lib) {
	carry = 0;	
	exit = false;
	init();
	mvprintw(10,10,"show library...)");
	int n = lv.showLibrary();
	if (n != -1) {
		ModelMaker maker;
		LibraryRecord lr = lib.getRecord(n);
		maker.makeModel(model, lr);	
		refresh();
		model.switchSource(0);
		first_line_shown = model.getLinesRed();
		AudioModule am = model.rusAudio;
		amv = am;
		show(2);
	} else {
		exit = true;
	}
	//doMainCycle();
}

void View::init() {
	setlocale(LC_ALL, "");
	std::cout <<"Init..." << std::endl;
	initscr();
	getmaxyx(stdscr, row, col);
	oldCol = col;
	col += 30;
	raw();
	cbreak();
	keypad(stdscr, TRUE);
	
}

void View::showByStrings() {
	
	clear();

	int i = 0;
	int j = first_line_shown;

	std::string prevLine = "";
	while (i < row && j < model.getBookSize()) {
		std::string currentLine = prevLine.compare("") == 0 ? model.getText(j) : prevLine + model.getText(j);
		
		if (currentLine.length() > row) {
			int k = 0;
			while ((k + 1)*col < currentLine.length()) {
				mvprintw(i,0,currentLine.substr(k*col, col).c_str());
				++k;
				++i;
				if (i == row) break;
			}
			mvprintw(i,0,currentLine.substr(k*col).c_str());
			++i;
			++j;
			prevLine = "";
		} else if (currentLine.length() < row - 10) {
			prevLine = currentLine;
			++j;
		}
		
	} 


	
}

void View::show(int isUp = 2) {
	
	//static int carry = 0;
	
	if (isUp == 0) {
		if (first_line_shown >= model.getBookSize() - 2) return;
		if ((carry+1)*col > model.getText(first_line_shown).length()) {
			carry = 0;
			++first_line_shown;
		} else {
			++carry;
		}
	} else if(isUp == 1) {
		if (carry == 0 && first_line_shown == 0) return;
		if (carry == 0) {
			--first_line_shown;
		} else {
			--carry;
		}
	}

	int i = 0;
	int j = first_line_shown;	
	clear();


	while (i < row && j < model.getBookSize()) {
		std::string currentLine = model.getText(j);
		
		try {
		if (i == 0 && carry > 0) 
				currentLine = currentLine.substr(col*(carry));
		}catch(...) {mvprintw(0,0,"%d %d",row,carry);return;}
		if (currentLine.length() < col) {
			
			mvprintw(i, 0, currentLine.c_str());	
			++i;
			++j;
		} else {
			
			int k = 0;
			int prevIndex = 0;
			while (k + col < currentLine.length()) {
				k += col;
				if (currentLine.at(k) != ' ') {
					int p = 0;
					int prev = k;
					while (currentLine.at(k) != ' ' && p < 10) {
						k += -1*(p + 1);
					}
					if (p == -9) k = prev;
				}
				mvprintw(i,0,currentLine.substr(prevIndex, k - prevIndex).c_str());
				++i;
				//if (j == first_line_shown && prevIndex == 0) carry = k;
				prevIndex = k;
				if (i == row) break;
			}
			
			mvprintw(i,0,currentLine.substr(prevIndex).c_str());
			++i;
			++j;
		}
			
			

	}
	
	refresh();
	
}



void View::showSwitchMenu() {
	ITEM ** items = (ITEM **) malloc(4*sizeof(ITEM * ));
	items[0] = new_item("Rus book", "     ");
	items[1] = new_item("Eng book", "     ");
	items[3] = new_item("<-","     ");
	items[2] = new_item("Audio book", "      ");
	items[4] = (ITEM *) NULL;
	
	//ITEM * curItem;
	MENU * menu = new_menu(items);
	post_menu(menu);
	refresh();
	int ch;
	int index = 1;
	while ((ch = getch()) != Settings::getInstance().getKey("V_END_KEY")) {
		switch (ch) {
			case KEY_UP : menu_driver(menu, REQ_UP_ITEM);
				      if (index > 1) --index;
				      break;
			case KEY_DOWN : menu_driver(menu, REQ_DOWN_ITEM);
					if (index < 4) ++index;
					break;
		}
	}

	switch (index) {
		case 1 : model.switchSource(0);
		         if (col <= oldCol) {
				col += V_COL_CORRECT;
			 } 
			 break;
		case 2 : model.switchSource(1);
		         if (col > oldCol) {
				col -= V_COL_CORRECT;
			 }
			 break;
		case 3: amv.getAudio().play();
			amv.playMode(row,oldCol,first_line_shown,model.getBookSize());
			break;
	}

}

void View::doMainCycle() {
	
	while (!exit) {
		int ch = getch();
		switch (ch) {

			case KEY_UP:
				if (first_line_shown > 0) {
				//	--first_line_shown;
				}
				show(1);
				break;
			case KEY_DOWN:
				if (first_line_shown <= model.getBookSize() - 2) {
				//	++first_line_shown;
				}
				show(0);
				break;
			case V_EXIT_KEY:
				exit = TRUE;
				break;
			case V_SWITCH_KEY:
				if (col > oldCol) {
					col -= V_COL_CORRECT;
				} else {
					col += V_COL_CORRECT;
				}
				model.setLinesRed(first_line_shown);
				model.switchSource();
				first_line_shown = model.getLinesRed();
				show();
				break;
			case V_SWITCH_MENU_KEY:
				carry = 0;
				showSwitchMenu();
				show();
				break;
			case V_PM_START:
				amv.getAudio().play();
				amv.playMode(row,oldCol,first_line_shown,model.getBookSize());
				show();
				break;

		}
		
	}
}


void View::showLibrary() {
	int n = lv.showLibrary();
	if (n == -1) return;
	ModelMaker maker;
	LibraryRecord lr = lv.getLibrary().getRecord(n);
	maker.makeModel(model, lr);	
	refresh();
	model.switchSource(0);
	first_line_shown = model.getLinesRed();
	AudioModule am = model.rusAudio;
	amv = am;
	show();
}


View::~View() {
	endwin();
}



